<template>
  <div>
    <Title title="敏感词库" font-size="16px" style="margin-bottom: 16px;"/>
    <div class="wrap">
      <div class="left">
        <div class="word">
          <span class="text">
            <span class="red-text">*</span>警告级别
          </span>
          <div class="content">
            <span v-if=" formData.warningWords" v-html="handleWords(formData.warningWords)"/>
            <span v-else class="placeholder">未填写敏感词</span>
          </div>
        </div>
        <el-button class="ok submit-btn" type="primary" size="small" @click="toEdit(0)">编辑</el-button>
      </div>
      <div>
        <div class="word">
          <span class="text">
            <span class="red-text">*</span>严禁级别
          </span>
          <div class="content">
            <span v-if="formData.forbiddenWords" v-html="handleWords(formData.forbiddenWords)"/>
            <span v-else class="placeholder">未填写敏感词</span>
          </div>
        </div>
        <el-button class="ok submit-btn" type="primary" size="small" @click="toEdit(1)">编辑</el-button>
      </div>
    </div>
    <el-dialog
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      class="small-common-dialog-size"
      @close="handleClose">
      <Title :title="`编辑${ editType ? '严禁' : '警告' }级别敏感词`" />
      <el-input v-model="word" placeholder="请输入" type="textarea" resize="none" rows="3" class="textarea"/>
      <div class="center">
        <el-button :loading="loading" type="primary" size="small" @click="setSensitiveWord(word, editType)">保存</el-button>
        <el-button type="info" size="small" class="cancel-btn-style" @click="handleClose">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Title from '@/components/Title';
import { mapState } from 'vuex';
import { setSensitiveWord, getSensitiveWord } from '@/api/setting';
export default {
  components: { Title },
  data() {
    return {
      dialogFormVisible: false,
      formData: {
        warningWords: '',
        forbiddenWords: ''
      },
      word: '',
      editType: 0,
      loading: false
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getSensitiveWord().then(res => {
        if (res.code === 200) {
          this.formData = res.data;
        }
      }).catch(err => {
        console.log(err);
      });
    },
    toEdit(type = 0) {
      this.editType = type;
      this.word = type ? this.formData.forbiddenWords : this.formData.warningWords;
      this.dialogFormVisible = true;
    },
    handleClose() {
      this.editType = 0;
      this.dialogFormVisible = false;
    },
    handleWords(words) {
      return words.replace(/\n/g, '<br>');
    },
    setSensitiveWord(sensitiveWord, type) {
      if (!sensitiveWord) {
        this.$message.error('请输入敏感词');
        return;
      }
      this.loading = true;
      setSensitiveWord({ sensitiveWord, type }).then(res => {
        if (res.code === 200) {
          this.$message.success('保存成功');
          this.fetchData();
          this.handleClose();
        }
        this.loading = false;
      }).catch(err => {
        this.loading = false;
        console.log(err);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.wrap {
  display: flex;
  align-items: center;
  margin-bottom: 32px;
  .left {
    margin-right: 120px;
  }
}
.word {
  display: flex;
  justify-content: flex-start;
  .text {
    width: 80px;
    text-align: right;
    margin: 6px 12px 0 0;
    font-size: 14px;
    font-family: PingFangSC-Regular;
    color: #666;
    .red-text {
      color: #FF6573;
      margin-right: 2px;
    }
  }
  .content {
    width: 400px;
    height: 240px;
    border: 0.5px solid #dddddd;
    border-radius: 8px;
    overflow-y: auto;
    padding: 6px;
    font-size: 14px;
    .placeholder {
      color: #999;
    }
  }
}
.ok {
  margin:16px 0 0 92px;
  background: #eff3fb;
  color: #406EFF;
  border: none;
}
.center {
  display: flex;
  justify-content: center;
  align-items: center;
}
.textarea {
  margin: 16px 0 24px 0;
  ::v-deep {
    .el-textarea__inner {
      width: 100%;
      height: 120px;
    }
  }
}
</style>
